package com.ec.generator.modules.gen.controller;

import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.mybatis.groups.ValidatorGroup;
import com.ec.generator.modules.gen.dto.ProjectDetailDTO;
import com.ec.generator.modules.gen.dto.QueryProjectDTO;
import com.ec.generator.modules.gen.service.IProjectDbService;
import com.ec.generator.modules.gen.service.IProjectService;
import com.ec.generator.modules.gen.vo.ProjectDbVO;
import com.ec.generator.modules.gen.vo.ProjectDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author Eric Lee
 * @time: 2022/1/17 20:08
 */
@RestController
@RequestMapping("/project")
@Slf4j
public class ProjectController {
    @Resource
    private IProjectService projectService;
    @Resource
    private IProjectDbService projectDbService;

    @GetMapping("/listProject")
    public Result<BasePageVO<ProjectDetailVO>> listProject(QueryProjectDTO queryProjectDTO) {
        return ResponseMsg.success(projectService.listProject(queryProjectDTO));
    }

    @PostMapping
    public Result<Void> addProject(@Validated(ValidatorGroup.Add.class) @RequestBody ProjectDetailDTO projectDTO) {
        return ResponseMsg.status(projectService.addProject(projectDTO));
    }

    @PutMapping
    public Result<Void> updateProject(@RequestBody ProjectDetailDTO projectDTO) {
        return ResponseMsg.status(projectService.updateProject(projectDTO));
    }

    @DeleteMapping("{projectId}")
    public Result<Void> removeProject(@PathVariable long projectId) {
        ProjectDetailDTO dto = new ProjectDetailDTO();
        dto.setProjectId(projectId);
        return ResponseMsg.status(projectService.delProject(dto));
    }

    @GetMapping("/{projectId}")
    public Result<List<ProjectDbVO>> projectDbs(@PathVariable Long projectId) {
        return ResponseMsg.success(projectDbService.listProjectDb(projectId));
    }
}
